﻿namespace CardManagement.Language
{
    using System;
    using System.Collections;
    using System.Data;
    using System.IO;
    using System.Windows.Forms;
    using System.Xml;

    internal class MultiLanguage
    {
        public static string DefaultLanguage = "English";

        public static string GetDefaultLanguage()
        {
            string innerText = "English";
            XmlReader reader = new XmlTextReader(@"Language\DefaultLanguage.xml");
            XmlDocument document = new XmlDocument();
            document.Load(reader);
            XmlNode node2 = document.DocumentElement.SelectSingleNode("DefaultLanguage");
            if (node2 != null)
            {
                innerText = node2.InnerText;
            }
            reader.Close();
            reader.Dispose();
            return innerText;
        }

        private static void GetSetListViewColumns(ListView.ColumnHeaderCollection items, Hashtable hashText)
        {
            try
            {
                foreach (ColumnHeader header in items)
                {
                    if (hashText.Contains(header.Name.ToLower()))
                    {
                        header.Text = (string) hashText[header.Name.ToLower()];
                    }
                }
            }
            catch (Exception exception1)
            {
                throw new Exception(exception1.Message);
            }
        }

        private static void GetSetSubControls(Control.ControlCollection controls, Hashtable hashText)
        {
            try
            {
                foreach (Control control in controls)
                {
                    if (control.GetType() == typeof(Panel))
                    {
                        GetSetSubControls(control.Controls, hashText);
                    }
                    else if (control.GetType() == typeof(GroupBox))
                    {
                        GetSetSubControls(control.Controls, hashText);
                    }
                    else if (control.GetType() == typeof(SplitContainer))
                    {
                        GetSetSubControls(control.Controls, hashText);
                    }
                    else if (control.GetType() == typeof(TabControl))
                    {
                        GetSetSubControls(control.Controls, hashText);
                    }
                    else if (control.GetType() == typeof(TabPage))
                    {
                        GetSetSubControls(control.Controls, hashText);
                    }
                    else if (control.GetType() == typeof(ListView))
                    {
                        GetSetListViewColumns(((ListView) control).Columns, hashText);
                    }
                    if (hashText.Contains(control.Name.ToLower()))
                    {
                        control.Text = (string) hashText[control.Name.ToLower()];
                    }
                }
            }
            catch (Exception exception1)
            {
                throw new Exception(exception1.Message);
            }
        }

        public static void LoadLanguage(ContainerControl form)
        {
            string defaultLanguage = GetDefaultLanguage();
            Hashtable objA = ReadXMLText(form.Name, defaultLanguage);
            if (!ReferenceEquals(objA, null))
            {
                Control.ControlCollection controls = form.Controls;
                try
                {
                    foreach (Control control in controls)
                    {
                        if (control.GetType() == typeof(TableLayoutPanel))
                        {
                            GetSetSubControls(control.Controls, objA);
                        }
                        if (control.GetType() == typeof(Panel))
                        {
                            GetSetSubControls(control.Controls, objA);
                        }
                        else if (control.GetType() == typeof(GroupBox))
                        {
                            GetSetSubControls(control.Controls, objA);
                        }
                        else if (control.GetType() == typeof(TabControl))
                        {
                            GetSetSubControls(control.Controls, objA);
                        }
                        else if (control.GetType() == typeof(TabPage))
                        {
                            GetSetSubControls(control.Controls, objA);
                        }
                        else if (control.GetType() == typeof(ListView))
                        {
                            GetSetListViewColumns(((ListView) control).Columns, objA);
                        }
                        if (objA.Contains(control.Name.ToLower()))
                        {
                            control.Text = (string) objA[control.Name.ToLower()];
                        }
                    }
                    if (objA.Contains(form.Name.ToLower()))
                    {
                        form.Text = (string) objA[form.Name.ToLower()];
                    }
                }
                catch
                {
                }
            }
        }

        private static Hashtable ReadXMLText(string frmName, string lang)
        {
            try
            {
                Hashtable hashtable = new Hashtable();
                XmlReader reader = null;
                if (new FileInfo(@"Language\" + lang + ".xml").Exists)
                {
                    reader = new XmlTextReader(@"Language\" + lang + ".xml");
                    XmlDocument document = new XmlDocument();
                    document.Load(reader);
                    foreach (XmlNode node2 in document.DocumentElement.SelectNodes("Form[Name='" + frmName + "']/Controls/Control"))
                    {
                        try
                        {
                            XmlNode node3 = node2.SelectSingleNode("@name");
                            XmlNode node4 = node2.SelectSingleNode("@text");
                            if (node3 != null)
                            {
                                hashtable.Add(node3.InnerText.ToLower(), node4.InnerText);
                            }
                        }
                        catch
                        {
                        }
                    }
                }
                else
                {
                    return null;
                }
                reader.Close();
                reader.Dispose();
                return hashtable;
            }
            catch
            {
                return null;
            }
        }

        public static void SetDefaultLanguage(string lang)
        {
            DataSet set = new DataSet();
            set.ReadXml(@"Language\DefaultLanguage.xml");
            set.Tables["CardManagement"].Rows[0]["DefaultLanguage"] = lang;
            set.AcceptChanges();
            set.WriteXml(@"Language\DefaultLanguage.xml");
            DefaultLanguage = lang;
        }
    }
}

